var app = getApp()
var pagesize = 10
var pageoffset
var payinfo
var orderindex
var reasons
var statusDescSets = {
  PAID: '已支付',
  UNPAID: '未支付',
  PRODUCING: '配送中',
  DISPATCHING: '配送中',
  RECEIVED: '已收货',
  CANCELLING: '已取消-未退款',
  CANCELLED: '已取消',
  TIMEOUT: '超时取消',
  NULLIFY: '未付款取消',
  COMPLETE: '已完成'
}

Page({
  data: {},
  onLoad: function (options) {
    var ordertype = '1'
    if (options.ordertype) {
      ordertype = options.ordertype
    }
    this.setData({
      ordertype: ordertype,
      orders: []
    })
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollhgt: res.windowHeight,
          scrolltop: 0
        })
      },
    })
    pageoffset = 1
    payinfo = []
  },
  onShow: function () {
    this.loadOrderlist(this.data.ordertype, pageoffset)
  },
  loadMoreOrder: function () {
    if (this.data.noMore == true) {
      return
    }
    pageoffset++
    this.loadOrderlist(this.data.ordertype, pageoffset)
  },

  changeOrderStatus: function (e) {
    var otype = e.currentTarget.dataset.otype
    this.setData({
      ordertype: otype
    })
    pageoffset = 1
    this.setData({
      noMore: false
    })
    this.loadOrderlist(otype, pageoffset)
  },
  loadOrderlist: function (ordertype, offset) {
    var url = '/yuanmai_we/orders/orderFormList/ALL'
    if (ordertype === '2') {
      url = '/yuanmai_we/orders/orderFormList/UNPAID'
    }
    else if (ordertype === '3') {
      url = '/yuanmai_we/orders/orderFormList/UNRECEIVED'
    }
    else if (ordertype === '4') {
      url = '/yuanmai_we/orders/orderFormList/REFUND'
    }
    url = url + '/' + pagesize + '/' + offset
    url = url + '?userId=' + app.globalData.userid
    app.showLoading()
    app.get(url).then(this.loadOrderlistCallBack.bind(this))
  },
  loadOrderlistCallBack: function (res) {
    app.hideLoading()

    if (res.length == null || res.length === undefined) {
      return
    }

    if (res.length < pagesize || res.length <= 0) {
      this.setData({
        noMore: true
      })
    }

    var orderlist = []
    if (pageoffset > 1) {
      orderlist = this.data.orders
    } else {
      this.setData({
        scrolltop: 0
      })
      payinfo = []
    }

    for (var i = 0; i < res.length; i++) {
      var goods = []
      var num = 0
      for (var j = 0; j < res[i].orderformOrderBillDOs[0].orderformOrderDetailDOs.length; j++) {
        var goodsinfo = {
          img: res[i].orderformOrderBillDOs[0].orderformOrderDetailDOs[j].IMAGE,
          num: res[i].orderformOrderBillDOs[0].orderformOrderDetailDOs[j].COUNT
        }
        num += parseInt(res[i].orderformOrderBillDOs[0].orderformOrderDetailDOs[j].COUNT)
        goods.push(goodsinfo)
      }
      var status = res[i].orderformOrderBillDOs[0].orderformOrderDO.STATUS
      var statusDesc = statusDescSets[status] ? statusDescSets[status] : status
      var orderinfo = {
        orderno: res[i].id,
        corderno: res[i].orderformOrderBillDOs[0].id,
        source: res[i].orderformBatchDO.ORDERSOURCE,
        statusDesc: statusDesc,
        status: status,
        goods: goods,
        num: num
      }
      orderlist.push(orderinfo)
      payinfo.push(res[i])
    }
    this.setData({
      orders: orderlist
    })
  },
  goBuyAction: function () {
    wx.switchTab({
      url: '../../bread/breadindex',
    })
  },
  payOrder: function (e) {
    var index = e.currentTarget.dataset.itemindex
    var orderinfo = encodeURIComponent(JSON.stringify(payinfo[index]))
    wx.navigateTo({
      url: '../../pay/pay?order=' + orderinfo,
    })
  },
  showDetail: function (e) {
    var gtype = e.currentTarget.dataset.gtype
    var index = e.currentTarget.dataset.itemindex
    orderindex = index
    if (gtype === 'MCAKE') {
      var orderno = this.data.orders[index].orderno
      var url = '/yuanmai_we/orders/mcake/status/query?id=' + orderno
      app.get(url).then(this.orderDetailCallBack.bind(this))
    } else {
      var orderinfo = encodeURIComponent(JSON.stringify(payinfo[orderindex]))
      var statusDesc = this.data.orders[orderindex].statusDesc
      wx.navigateTo({
        url: './orderdetail?order=' + orderinfo + '&status=' + statusDesc + '&type=bread&orderSource=online',
      })
    }
  },
  orderDetailCallBack: function (res) {
    var orderinfo = encodeURIComponent(JSON.stringify(payinfo[orderindex]))
    var statusDesc = this.data.orders[orderindex].statusDesc
    wx.navigateTo({
      url: './orderdetail?order=' + orderinfo + '&status=' + statusDesc + '&type=cake&orderSource=online',
    })
  },
  cancelOrder: function (e) {
    var index = e.currentTarget.dataset.itemindex
    orderindex = index
    var that = this
    wx.showModal({
      title: '',
      content: '是否取消订单?',
      confirmText: '是',
      confirmColor: '#a08257',
      cancelText: '否',
      success: function (res) {
        if (res.confirm) {
          that.didCancelAction1()
        }
      }
    })
  },
  didCancelAction1: function () {
    var url = '/yuanmai_we/orderform/order/cancel/reason'
    app.get(url).then(res => {
      reasons = res
      var reasons_ch = []
      for (var i = 0; i < reasons.length; i++) {
        reasons_ch.push(reasons[i].REASON)
      }
      var that = this
      wx.showActionSheet({
        itemList: reasons_ch,
        success: function (res) {
          that.didCancelAction2(res.tapIndex)
        }
      })
    })
  },
  didCancelAction2: function (index) {
    var url = '/yuanmai_we/orderform/orderForm/cancel/'
    var orderno = this.data.orders[orderindex].corderno
    url = url + orderno
    var params = {
      REASON: reasons[index].REASON,
      __zaofans: true
    }
    app.request(url, params).then(res => {
      if (res.success === true) {
        this.loadOrderlist(this.data.ordertype, pageoffset)
      }
    })
  }
})
